package com.shouer.doc.ui.userinfo.presenter;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.shouer.doc.common.ShouerConst;
import com.shouer.doc.request.GsonRequest;
import com.shouer.doc.request.RequestUtil;
import com.shouer.doc.ui.userinfo.response.GetCashApplyResponse;
import com.shouer.doc.ui.userinfo.response.QueryLastBillResponse;
import com.shouer.doc.ui.userinfo.view.IUserInfoView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by YYY on 2016/12/18.
 */

public class UserInfoPresenterCompl implements IUserInfoPresenter {

    private IUserInfoView userInfoView;
    private boolean isRefreshing= false;

    public UserInfoPresenterCompl(IUserInfoView userInfoView) {
        this.userInfoView = userInfoView;
        EventBus.getDefault().register(this);
    }

    @Override
    public void queryBill() {

        Map<String, String> param = new HashMap<>();
        param.put("pageNo", String.valueOf(1));
        param.put("pageNum", String.valueOf(10));
        param.put("docId",userInfoView.getUser().getUserId());
        isRefreshing = true;

        final GsonRequest<QueryLastBillResponse> request = new GsonRequest("queryBill", QueryLastBillResponse.class, new Response.Listener<QueryLastBillResponse>() {
            @Override
            public void onResponse(QueryLastBillResponse response) {

                EventBus.getDefault().post(response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                isRefreshing = false;
                userInfoView.queryLastBillFailed("网络异常 查询失败");
            }
        });

        RequestUtil.sendRequest(userInfoView.getContext(), param, request);

    }

    @Override
    public boolean refresh() {

        if (isRefreshing) {
            return false;
        }
        queryBill();
        return true;
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        userInfoView=null;
    }

    @Override
    public void getCashApply(String getCashAmount) {
        Map<String, String> param = new HashMap<>();
        param.put("docId", userInfoView.getUser().getUserId());
        param.put("getCashAmount", getCashAmount);

        final GsonRequest<GetCashApplyResponse> request = new GsonRequest(ShouerConst.APP_SERVER_https,"getCashApply", GetCashApplyResponse.class, new Response.Listener<GetCashApplyResponse>() {
            @Override
            public void onResponse(GetCashApplyResponse response) {

                EventBus.getDefault().post(response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                userInfoView.getCashApplyFailed("网络异常  申请体现失败");
            }
        });

        RequestUtil.sendRequest(userInfoView.getContext(), param, request);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(QueryLastBillResponse response)
    {
        isRefreshing=false;
        if (response.getErrorCode() >= 0) {
            userInfoView.queryLastBillSuccess(response.getBalance(),response.getBillList());
        } else {
            userInfoView.queryLastBillFailed(response.getErrorMsg());
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(GetCashApplyResponse response)
    {

        if (response.getErrorCode()>=0)
        {
            userInfoView.getCashApplySuccess();
        }else
        {
            userInfoView.getCashApplyFailed(response.getErrorMsg());
        }

    }
}
